<?php
/**
 * Created by PhpStorm.
 * User: zengyuming
 * Date: 2017/7/25
 * Time: 下午3:10
 */
namespace Api\Controller;
class OpenidController extends CommonController{
    public function getPayOpenid($pay_id, $user_id, $merchant_id, $redirect_url){
        $openid = M('user_extend')->where(array('user_id' => $user_id, 'mch_id' => $merchant_id, 'pay_id' => $pay_id))->getField('wx_recharge_openid');
//        var_dump($openid . __METHOD__);die;
        $k = md5('key=' . C("SignKey") . '&id=' . $pay_id . $user_id);


        /*
         *
         *
         *  wx59c22bb7d374a032   65
            wx3a3b8d4239d05969   54
            wx69b78d36cda2468f    55
            wx610d9c4e11dcc91e    56
            wx6e4b0a842c29e9d3   57
            wxcc09581ab1d51e61    58
            wx6cb3004acdd60e7d    59
            wxc55f3fbfa2aec885    61
            wx8636c7a0cda03f92   62
         *
         *
         * */

        if (empty($openid)) {
            $pay_idArr = range(13, 62);
            $zhihao = array(24 , 41 , 42 , 43 , 44 , 10 , 3 , 5 , 6 , 8 , 9 , 11 , 12 , 24 , 65 , 54 , 55 , 56 , 57 , 58 , 59 , 61 , 62 , 67 , 68 , 71 , 72 , 73 , 74 , 75 , 77 , 78 , 80); // 志豪这边的openid
            foreach($pay_idArr as $arrKey => $val){
                if(in_array($val , $zhihao)){
                    unset($pay_idArr[$arrKey]);
                }
            }

            if (in_array($pay_id, $pay_idArr)) {
                $this->ThreefloorOpenid($pay_id, $k, $user_id, $merchant_id, $redirect_url);
            }elseif(in_array($pay_id , $zhihao)){
                $this->requestRechargeOpenid_gzh_charge($pay_id, $k, $user_id, $merchant_id, $redirect_url);
            }
        }else{
            exit("Openid Already exists");
        }
    }


    //获取犇付_俊翼微信直充openid
    private function requestRechargeOpenid_gzh_charge($id, $sign1, $user_id, $merchant_id, $redirect_url){
        if (md5('key='. C("SignKey") .'&id=' . $id . $user_id) != $sign1) exit();
        if (empty($user_id)) {
            return false;
        }

        $pay_id = $id;
        $auth_url = 'http://auth.djblc.cn/IndexB/requestRecharge_new_testym';
        $key = 'aabc2B010d96DA1fa3aa99B6cebc34fa';

        $callback = 'http://' . $_SERVER['HTTP_HOST'] . '/Api/Openid/setRechargeOpenidCallback';
        $project_name = 'centerpay';

        $time = time();
        $sign = md5($user_id . $callback . $project_name . $merchant_id . $redirect_url . $time . $key);
        $url = $auth_url . "?user_id={$user_id}&callback={$callback}&project_name={$project_name}&time={$time}&sign={$sign}&pay_id={$pay_id}&merchant_id={$merchant_id}&redirect_url=" . base64_encode($redirect_url);
        header("Location:{$url}");
        exit;
    }


// openid获取  三楼阿文获取
    private function ThreefloorOpenid($pay_id, $sign, $user_id, $merchant_id, $redirect_url){
        if ($pay_id == '13') {
            // 珠海市尔凯斯
            $appid = 'wx4a3e4896ef9a0ec3';
            $appsecret = 'a3dced975afa0634cac888d5791e9577';
        } elseif ($pay_id == '14') {
            // 霸耀票务
            $appid = "wx4c0d115e30a3e235";
            $appsecret = "92f62c4e039fe6b2d08482fd4f5f2a6d";
        } elseif ($pay_id == '15') {
            // 黎悦科技
            $appid = "wxf2e639651e741578";
            $appsecret = "92d6e64e19a78bc774e5209b8a7540d1";
        } elseif ($pay_id == '16') {
            // 天韵贸易
            $appid = "wx4732703dd01d7d5f";
            $appsecret = "62c10fa9cddea2f55249d6a4bb15de18";
        } elseif ($pay_id == '17') {
            // 燕特
            $appid = 'wx8a1fd9d8be1fe21c';
            $appsecret = "f1cea847c1f4c72949f2c17ec9bfdfbc";
        } elseif ($pay_id == '18') {
            // 珠海市源莱宝网络科技有限公司
            $appid = 'wxf44e91b1394e6b60';
            $appsecret = "d3eb765e009c977ae7c0f123eb09189d";
        } elseif ($pay_id == '19') {
            // 三明市杰信电子商务有限公司
            $appid = 'wxd1123d80db54b297';
            $appsecret = "29688b2056d6211ea150f74098a67a06";
        } elseif ($pay_id == '20') {
            // 广州市天河区林和卓信电子经营部
            $appid = 'wxf21da8fce5b2da16';
            $appsecret = '3b4893768933dba267c0ad87dd0e6171';
        } elseif ($pay_id == '21') {
            // 广州市天河区林和锐志电子经营部
            $appid = 'wx61b94d5a076baa85';
            $appsecret = '9b1c7e4570c3dd980f75df2b78654de1';
//        }elseif($pay_id == '22') {
//            // 广州市天河区黄村瑞迪电子产品经营部
//            $appid = 'wx782d8b7df779811f';
//            $appsecret = '6c612a9752f4218bc18135d8e45f9754';
        } elseif ($pay_id == '23') {
            // 福安市银盛电子产品经营部
            $appid = 'wx4cdb08a27e9937ca';
            $appsecret = '30bec1ae844829f75298d557681982dc';
        } elseif ($pay_id == '25') {
            // 燕特2
            $appid = 'wx3a976ee10845f06d';
            $appsecret = '03fcb53c8b4f4d5988c0cff872aafd5c';
        } elseif ($pay_id == '26') {
            // 小财鸡
            $appid = 'wxb5909706111ce3f1';
            $appsecret = 'a926aa9f5d67c78419ea8b2fdc4db0f4';

        } elseif ($pay_id == '28') {
            // 三明市梅列区福兴利电脑经营部
            $appid = 'wx2bb0f929f18cb9ba';
            $appsecret = '592d7ce5f16d936a4e1cec4239f51a45';

        } elseif ($pay_id == '29') {
            // 三明市梅列区乐购数码产品店
            $appid = 'wx3bc28aad3143367a';
            $appsecret = 'e825f5b63365e500f2461b163c2c28f8';

        } elseif ($pay_id == '30') {
            // 三明市梅列区金渡电子产品店
            $appid = 'wx74bad5e8624edcbb';
            $appsecret = '0c821a64507547957ce8637cc85b58ea';

        } elseif ($pay_id == '31') {
            // 三明市星米电子商务有限公司
            $appid = 'wx8adb10a49fad5d52';
            $appsecret = '8f2dad2899286b83ef3d6f0d1709ec62';

        } elseif ($pay_id == '32') {
            // 福安市红创电子商务信息咨询服务部
            $appid = 'wx97a116e0b3ebaadf';
            $appsecret = 'f876755797826ec1d566a8071a9b6913';

        } elseif ($pay_id == '33') {
            // 锐志小葵花助手公众号
            $appid = 'wx61b94d5a076baa85';
            $appsecret = '9b1c7e4570c3dd980f75df2b78654de1';


            // 8月1号

        } elseif ($pay_id == '34') {
            // 三明市梅列区菲美雅贸易商行
            $appid = 'wx68a043e401ef913e';
            $appsecret = '1bbd1c9522d9b556a84702b955d015cb';

        } elseif ($pay_id == '35') {
            // 三明市三元区金凯利贸易商行
            $appid = 'wx17afd425dc741aef';
            $appsecret = 'ab0693cf0cf869592f637d4feb2146be';

        } elseif ($pay_id == '36') {
            // 三明市三元区岚香坊贸易商行
            $appid = 'wx1277267c92bed0e8';
            $appsecret = '1b4a0e4e05d46b9f0c867dfb459c9b3e';

        } elseif ($pay_id == '37') {
            // 三明市梅列区焱佳贸易商行
            $appid = 'wx7f96c65a59a63a0d';
            $appsecret = 'a35767a39bb36ab627c0da2f29840d25';
        } elseif ($pay_id == '38') {
            // 三明市三元区善焱贸易商行
            $appid = 'wxd22aa85b0c5d5247';
            $appsecret = '0fc6d4ae9c0b33b9d898dd2617ead28d';
        }elseif($pay_id == '45') {
            // 通联翼宝科技
            $appid = 'wx97a2aebdf8fe8b74';
            $appsecret = 'd5530b5a700f23db6385aaf1a062d39e';
        }elseif($pay_id == '47') {
            // 福安市星宏电子商务经营部
            $appid = 'wxe9a6932a233931bd';
            $appsecret = '20ea0211c59b32e42cb742634109026e';
        }elseif($pay_id == '48') {
            // 卓信巅峰科技公众号
            $appid = 'wxdb05684e821e81b6';
            $appsecret = 'a55578c8c4ffa38be4a97f8d5204cb80';
        }elseif($pay_id == '49') {
            // 广州明悦科技有限公司
            $appid = 'wxfa59fd8b0b657088';
            $appsecret = 'b855083d86e4c2aada8fca4c7ab3f08f';
        }elseif($pay_id == '49') {
            // 广州明悦科技有限公司
            $appid = 'wxfa59fd8b0b657088';
            $appsecret = 'b855083d86e4c2aada8fca4c7ab3f08f';
        }elseif($pay_id == '50') {
            // 卓信科技（雪东）
            $appid = 'wxf21da8fce5b2da16';
            $appsecret = '3b4893768933dba267c0ad87dd0e6171';
        }elseif($pay_id == '51') {
            // 巅峰科技（雪东）
            $appid = 'wxdb05684e821e81b6';
            $appsecret = 'a55578c8c4ffa38be4a97f8d5204cb80';
        }elseif($pay_id == '52') {
            // 银盛科技（嘉伟）
            $appid = 'wxab177625603ff654';
            $appsecret = '8f0118d44a9b492bd463d8f65bc4e309';
        }elseif($pay_id == '53') {
            // MG乐购（嘉伟）
            $appid = 'wx4cdb08a27e9937ca';
            $appsecret = '30bec1ae844829f75298d557681982dc';
        }
        if (md5('key=' . C("SignKey") . '&id=' . $pay_id . $user_id) != $sign) exit();
        $callbackUrl = S_URL . '/Api/Openid/ThreefloorOpenidCallback';
        $appid = \Think\Crypt\Driver\Think::encrypt($appid, '#changbao#');
        $appsecret = \Think\Crypt\Driver\Think::encrypt($appsecret, '#changbao#');

        $data = array(
            'pay_id' => $pay_id,
            'merchant_id' => $merchant_id,
            'user_id' => $user_id,
            'redirect_url' => $redirect_url
        ); // 自定义数据
        $params = \Think\Crypt\Driver\Think::encrypt(json_encode($data), '#changbao#');
        $parm = array(
            'appid' => $appid,
            'appsecret' => $appsecret,
            'callback' => $callbackUrl,
            'params' => $params,
        );

        $url = 'http://www.butimu.cn/Home/Public/getOutOpenid1?' . http_build_query($parm);
        header("location:" . $url);
    }


    //接受微信直充openid
    public function setRechargeOpenidCallback(){
        if (!empty($_GET)) {
            $str = json_encode($_GET);
            \Think\Log::write($str, 'INFO', '', LOG_PATH . "Api/getOpenid/" . date('Y-m-d') . ".log");
        }
        $user_id = intval(I('get.user_id'));
        $open_id = I('get.open_id');
        $project_name = I('get.project_name');
        $time = I('get.time');
        $sign = I('get.sign');
        $pay_id = I('get.pay_id');
        $merchant_id = I('get.merchant_id');
        $redirect_url = I('get.redirect_url');
//        dump(base64_decode($redirect_url));die;
        $key = 'aabc2B010d96DA1fa3aa99B6cebc34fa';
        $ver_sign = md5($user_id . $open_id . $project_name . $time . $key);
//        $ver_sign     = md5($user_id.$open_id.$project_name.$merchant_id.$redirect_url.$time.$key);
        if ($ver_sign == $sign) {
            if (!$res = M('user_extend')->where(array('user_id' => $user_id, 'mch_id' => $merchant_id, 'pay_id' => $pay_id))->find()) {
                $res = M('user_extend')->add(array('user_id' => $user_id, 'mch_id' => $merchant_id, 'wx_recharge_openid' => $open_id, 'pay_id' => $pay_id));
                header("Location:" . base64_decode($redirect_url));
            }
//            $this->redirect('/Home/Index');
        } else {
            $str = json_encode($_GET);
            \Think\Log::write($str, 'INFO', '', LOG_PATH . "Api/getOpenid/" . date('Y-m-d') . ".log");
//            $this->redirect('/Home/Index');
            echo 'openid failure';
        }

    }

// 三楼获取openid回调
    public function ThreefloorOpenidCallback(){
        $Openid = I('get.openid');
        $params = \Think\Crypt\Driver\Think::decrypt((base64_decode(I('get.params'))), '#changbao#');
        $Mysign = md5($Openid . I('get.params') . 'safpojaspoasgadgadgagjmmjgahg');
        if (I('get.sign') != $Mysign) exit('sign error');
        $params = json_decode($params, true);
        if (isset($Openid)) {
            $addres = M('user_extend')->add(array('user_id' => $params['user_id'], 'mch_id' => $params['merchant_id'], 'pay_id' => $params['pay_id'], 'wx_recharge_openid' => $Openid));
            if($addres){
                header("Location:" . $params['redirect_url']);
                exit();
            }
        }
        exit('openid存储失败');
    }

    /* 杉德Openid获取方法 */
    public function ShandeGetopenid(){
        $Parames = I("request.Parames");
        $CallbackContinuedata = json_decode(\Think\Crypt\Driver\Think::decrypt($Parames, C('encryptKey')) , true);
        if(I("get.Action") == 'CallbackOpenid'){
            $Openid = I("get.open_id");
            $sub_open_id = I("get.sub_open_id");
            if(strlen($Openid) > 28 || strlen($sub_open_id) > 28) exit("Openid error");
            $openid = array(
                'open_id' => $Openid,
                'sub_open_id' => $sub_open_id,
            );
            # 得到openid后将参数回传
            $payobj = new \Api\Controller\CreatePayController(); // 方便在传构造方法中传参数

//            $payobj->AllPayData = array(
//                'transactionData' => $CallbackContinuedata['transactionData'],
//                'get_data' => $CallbackContinuedata['get_data'] ,
//                'payName' => $CallbackContinuedata['payName'],
//                'mch_id' => $CallbackContinuedata['mch_id'],
//                'PayChannel_MethodName' => 'ShandeGzhPay',
//            ); // 这个数组是存准备调用的通道方法名 商户号 、 订单号 、 订单信息 、 金额等等;

            $payobj->ShandeGzhPay($CallbackContinuedata['get_data'] , $openid , $CallbackContinuedata['mch']);
        }
        # 获取openid
        $key = $CallbackContinuedata['mch']['mch_key'];
        $Basedata = array(
            'app_id' => $CallbackContinuedata['mch']['mch_id'],
            'method' => 'openapi.payment.auth.auth',
            'nonce' => rand(111111,999999),
            'format' => 'json',
            'sign_method' => 'md5',
            'biz_content' => NULL,
        );

        $biz_content = array(
            'url' => S_URL . '/Api/Openid/ShandeGetopenid?Action=CallbackOpenid&Parames=' . $Parames,
        );
        $Basedata['biz_content'] = json_encode($biz_content);
        $Basedata['sign'] = \Com\Pay\ShandeGzhPay::ShandeSetSign($Basedata , $key);
        $result = json_decode(curl_json_post('https://shq-api.51fubei.com/gateway' , json_encode($Basedata)) , true);
        if($result['data']['authUrl'] != '') {
            header("Location: " . $result['data']['authUrl']);
        }else{
            echo '杉德Openid获取失败';
            exit;
        }

    }
}